package com.yogee.topseller.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.adapter.SearchListHAdapter;
import com.yogee.topseller.bean.ModifyFastQueryLabelBean;
import com.yogee.topseller.bean.TagBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.RefreshUtil;
import com.yogee.topseller.view.ClearEditText;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 标签搜索界面
 *
 * @author hanyu
 *         created by 2017/7/17 11:41
 */

public class TagSearchHActivity extends HttpActivity implements RefreshUtil.OnRefreshListener, SearchListHAdapter.OnItemClick {

    @BindView(R.id.cet_input_text)
    EditText cetInputText;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.rv_search_list)
    RecyclerView rvSearchList;
    @BindView(R.id.refreshLayout)
    TwinklingRefreshLayout refreshLayout;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    private List<TagBean.LabelBean> beans;
    private SearchListHAdapter adapter;
    private boolean isRefresh = true;
    private String mCount = "10";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tag_search_h;
    }

    @Override
    protected void initView() {
        beans = new ArrayList<>();
        cetInputText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                loadSearchData("0", mCount);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        adapter = new SearchListHAdapter(this, this);
        rvSearchList.setLayoutManager(new LinearLayoutManager(this));
        rvSearchList.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new RefreshUtil(this).refreshListenerAdapter());
    }

    @OnClick(R.id.tv_cancel)
    public void onViewClicked() {
        //取消按钮事件
        finish();
    }

    @Override
    public void onRefresh() {//刷新
        isRefresh = true;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.finishRefreshing();
            }
        }, 1500);
        loadSearchData("0", mCount);
    }

    @Override
    public void onLoad() {//加载
        isRefresh = false;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.finishLoadmore();
            }
        }, 1500);
        loadSearchData(beans.size() + "", mCount);
    }

    @Override
    public void OnItemClickImp(String id,String name) {
        Intent mIntent = new Intent();
        mIntent.putExtra("id", id);
        mIntent.putExtra("name",name);
        // 设置结果，并进行传送  
        this.setResult(100, mIntent);
        finish();
    }

    /**
     * 加载搜索列表
     */
    private void loadSearchData(String total, String count) {
        HttpManager.getInstance().searchLabel(AppUtil.getUserId(this),cetInputText.getText().toString(), total, count).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<TagBean>() {
            @Override
            public void onNext(TagBean o) {
                loadingFinished();
                if (0 == o.getLabelList().size()) {
                    tvNoData.setVisibility(View.VISIBLE);
                    refreshLayout.setVisibility(View.GONE);
                } else {
                    tvNoData.setVisibility(View.GONE);
                    refreshLayout.setVisibility(View.VISIBLE);
                }
                if (isRefresh) {
                    refreshLayout.finishRefreshing();
                    beans = o.getLabelList();
                } else {
                    refreshLayout.finishLoadmore();
                    beans.addAll(o.getLabelList());
                }
                adapter.addData(beans);
            }
        }, this));
    }
}